package smedi.ismedi.mobile.bff.controller.inmail;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.inmail.api.dto.request.*;
import com.smedi.ismedi.inmail.api.dto.response.AnnouncementManagementResponse;
import com.smedi.ismedi.inmail.api.dto.response.AnnouncementResponse;
import com.smedi.ismedi.inmail.api.dto.response.NodeResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteAnnouncementFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 通知公告BFF
 *
 * @author jiangqinxuan
 * @date 2021/08/03
 */
@Slf4j
@Api(value = "通知公告接口", tags = {"通知公告接口"})
@RequestMapping(value = "/mobilebff/v1/announcement")
@RestController
@RequiredArgsConstructor
public class AnnouncementController {
    private final RemoteAnnouncementFeignApi remoteAnnouncementFeignApi;

    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 新建通知公告
     *
     * @param announcementCreateRequest 通知公告新建请求
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "新建通知公告")
    @PostMapping(value = "/create")
    ResponseData<AnnouncementResponse> createAnnouncement(@Validated @RequestBody AnnouncementCreateRequest announcementCreateRequest){
        return remoteAnnouncementFeignApi.createAnnouncement(announcementCreateRequest);
    }

    /**
     * 编辑通知公告
     *
     * @param announcementEditRequest 通知公告编辑请求
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "编辑通知公告")
    @PostMapping(value = "/edit")
    ResponseData<AnnouncementResponse> editAnnouncement(@Validated @RequestBody AnnouncementEditRequest announcementEditRequest){
        return remoteAnnouncementFeignApi.editAnnouncement(announcementEditRequest);
    }

    /**
     * 审核通知公告
     *
     * @param announcementReviewRequest 通知公告审核请求
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "审核通知公告")
    @PostMapping(value = "/review")
    ResponseData<AnnouncementResponse> reviewAnnouncement(@Validated @RequestBody AnnouncementReviewRequest announcementReviewRequest){
        return remoteAnnouncementFeignApi.reviewAnnouncement(announcementReviewRequest);
    }

    /**
     * 失效通知公告
     *
     * @param announcementDisableRequest 通知公告失效请求
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "失效通知公告")
    @PostMapping(value = "/disable")
    ResponseData<AnnouncementResponse> disableAnnouncement(@Validated @RequestBody AnnouncementDisableRequest announcementDisableRequest){
        return remoteAnnouncementFeignApi.disableAnnouncement(announcementDisableRequest);
    }

    /**
     * 获取 通知公告 单一（详情）
     *
     * @param announcementOneQueryRequest 通知公告单一查询
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "获取 通知公告 单一（详情）")
    @PostMapping(value = "/one")
    ResponseData<AnnouncementResponse> getAnnouncementOne(@Validated @RequestBody AnnouncementOneQueryRequest announcementOneQueryRequest){
        return remoteAnnouncementFeignApi.getAnnouncementOne(announcementOneQueryRequest);
    }

    /**
     * 获取 组织树
     *
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "GET", value = "获取 组织树")
    @GetMapping(value = "/getOrganizationTree")
    ResponseData<NodeResponse> getOrganizationTree(){
        return remoteAnnouncementFeignApi.getOrganizationTree();
    }

    /**
     * 获取 项目树
     *
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "GET", value = "获取 项目树")
    @GetMapping(value = "/getProjectTree")
    ResponseData<NodeResponse> getProjectTree(){
        return remoteAnnouncementFeignApi.getProjectTree();
    }

    /**
     * 获取 通知公告 分页
     *
     * @param announcementPageQueryRequest 通知公告列表查询
     * @return ResponseData<AnnouncementResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "获取 通知公告 分页")
    @PostMapping(value = "/page")
    ResponseData<PaginationData<AnnouncementResponse>> getAnnouncementPage(@Validated @RequestBody AnnouncementPageQueryRequest announcementPageQueryRequest){
        return remoteAnnouncementFeignApi.getAnnouncementPage(announcementPageQueryRequest);
    }

    /**
     * 获取文件信息
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "获取文件信息", nickname = "获取文件信息", response = ResponseData.class)
    @GetMapping(value = "/getFileInfoBySignKey")
    ResponseData<FileInfoResponse> getFileInfo(@ApiParam(value = "signKey", required = true) @RequestParam(value = "signKey", required = true) String signKey) {
        ResponseData<FileInfoResponse> fileInfo = fileFeignApi.getFileInfo(signKey);
        if (!fileInfo.isSuccess()) {
            return null;
        }
        String path = fileInfo.getData().getPath();
        String storageGroup = fileInfo.getData().getStorageGroup();
        fileInfo.getData().setPath(FDFS_SERVICE_ADDRESS_OUT + storageGroup + "/" + path);
        return fileInfo;
    }

    /**
     * 当前登录人是否有权限新增通知公告（临时接口）
     *
     * @return ResponseEntity<Boolean>
     */
    @ApiOperation(httpMethod = "GET", value = "当前登录人是否有权限新增通知公告（临时接口）")
    @GetMapping(value = "/getAccessToIncrease")
    ResponseData<Boolean> getAccessToIncrease(){
        return remoteAnnouncementFeignApi.getAccessToIncrease();
    }
}
